package utils

import "sync"

// 构建树工具类
type treeUtils struct{}

var (
	treeUtilsTemp *treeUtils
	treeUtilsOnce sync.Once
)

func NewTreeUtils() *treeUtils {
	treeUtilsOnce.Do(func() {
		treeUtilsTemp = &treeUtils{}
	})
	return treeUtilsTemp
}

// 菜单节点结构
type TreeNode struct {
	ID       int         `json:"id,omitempty"`
	Label    string      `json:"label,omitempty"`
	ParentId int         `json:"parentId,omitempty"`
	Weight   int         `json:"weight,omitempty"`
	Args     any         `json:"args,omitempty"`
	Children []*TreeNode `json:"children,omitempty"`
}

// GetTreeRecursive 指定根节点
func (m *treeUtils) GetTreeRecursive(list []*TreeNode, parentId int) []*TreeNode {
	res := make([]*TreeNode, 0)
	for _, v := range list {
		if v.ParentId == parentId {
			v.Children = m.GetTreeRecursive(list, v.ID)
			res = append(res, v)
		}
	}
	return res
}
